/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.server.data.img;

import hope.platform.data.dao.SiteConnectionInfoRepository;
import hope.platform.data.domain.SiteConnectionInfo;
import hope.platform.data.server.data.header.SiteConnectionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/12 8:56
 */
@Service
public class SiteConnectionInfoServiceImg implements SiteConnectionInfoService {

    @Autowired
    private SiteConnectionInfoRepository repository;

    @Override
    public SiteConnectionInfo addConnectionInfo(SiteConnectionInfo siteConnectionInfo) {
        siteConnectionInfo.setGmtCreate(new Date());
        siteConnectionInfo.setGmtModified(new Date());
        return repository.save(siteConnectionInfo);
    }

    @Override
    public SiteConnectionInfo updateConnectionInfo(SiteConnectionInfo siteConnectionInfo) {
        SiteConnectionInfo tmp = repository.findBySerialNumber(siteConnectionInfo.getSerialNumber());
        if(siteConnectionInfo.getClientConnectInfo()!= null){
            tmp.setClientConnectInfo(siteConnectionInfo.getClientConnectInfo());
        }
        if (siteConnectionInfo.getDataKindInfoMap() != null) {
            tmp.setDataKindInfoMap(siteConnectionInfo.getDataKindInfoMap());
        }
        tmp.setGmtModified(new Date());
        return repository.save(tmp);
    }

    @Override
    public SiteConnectionInfo getBySerialNumber(String serialNumber) {
        return repository.findBySerialNumber(serialNumber);
    }

    @Override
    public List<SiteConnectionInfo> getAll() {
        return repository.findAll();
    }
}
